package com.bsj.media.init;

import com.bsj.media.tool.HttpPlayBackTool;
import com.bsj.media.tool.HttpTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 用于延迟关闭
 */
@Slf4j
public class CloseThread {

    private static Thread thread;

    private static boolean flag = true;

    private static Map<String, Long> closeStr = new ConcurrentHashMap<>();

    public static void pushChannel(String simChannel) {
        if (!StringUtils.isEmpty(simChannel)) {
            closeStr.put(simChannel, System.currentTimeMillis());
        }
    }

    public static void removeChannel(String simChannel) {
        closeStr.remove(simChannel);
    }

    public static void start() {
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (flag) {
                    Long aLong = doRun();
                    long t1 = 1000 - aLong;
                    t1 = t1 < 0 ? 0 : t1;
                    try {
                        Thread.sleep(t1);
                    } catch (InterruptedException e) {

                    }
                }
            }
        });
        log.info("初始化延迟关闭视频线程...");
        thread.start();
    }

    private static Long doRun() {
        Set<String> delSet = new HashSet<>();
        long time = System.currentTimeMillis();
        if (closeStr != null && !closeStr.isEmpty()) {
            for (Map.Entry<String, Long> entry : closeStr.entrySet()) {
                Long value = entry.getValue();
                //大于15秒
                if (time - value > 15 * 1000) {
                    delSet.add(entry.getKey());
                }
            }
            for (String simChannel : delSet) {
                String[] arr = simChannel.split("-");
                int channel = Integer.parseInt(arr[1]);
                int port = 0;
                if (arr.length == 3) {
                    //回放
                    port = Integer.parseInt(arr[2]);
                }
                closeStr.remove(simChannel);
                if (checkOther(arr[0], channel, port)) {
                    if (port == 0) {
                        //直播关闭
                        HttpTool.closeMedia(arr[0], channel);
                    } else {
                        //回放关闭
                        HttpPlayBackTool.closePlayBack(port, arr[0], channel - 126);
                    }
                }
            }
        }
        return System.currentTimeMillis() - time;
    }

    public static boolean checkOther(String sim, int channel, int port) {
        if (channel == 1) {
            channel = 127;
        } else if (channel == 127) {
            channel = 1;
        }
        String simChannel = sim + "-" +  channel;
        if (port != 0) {
            simChannel = simChannel + "-" + port;
        }
        SendCachedData sendCachedData = SendChannelUntil.gainObj(simChannel);
        log.info("判断是否关闭指令:" + simChannel);
        if (sendCachedData != null && !sendCachedData.clearFlag()) {
            return false;
        } else {
            return true;
        }
    }

    public static void stop() {
        flag = false;
        if (thread != null) {
            thread.interrupt();
        }
    }
}
